package com.soilflow.info.service.impl;

import com.soilflow.common.constant.UserConstants;
import com.soilflow.common.core.domain.TreeSelect;
import com.soilflow.common.domain.dto.LandClassifyDto;
import com.soilflow.common.domain.vo.LandClassifyVo;
import com.soilflow.common.utils.StringUtils;
import com.soilflow.info.service.LandClassifyService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.soilflow.info.mapper.LandClassifyMapper;
import com.soilflow.common.domain.LandClassify;
import org.springframework.transaction.annotation.Transactional;;
/**
@author dhp 
@date 2023/4/21 - 14:17
*/
@Service
public class LandClassifyServiceImpl extends ServiceImpl<LandClassifyMapper, LandClassify> implements LandClassifyService {

    @Resource
    private LandClassifyMapper reliefClassifyMapper;

    /**
     * 查询分类列表
     *
     * @param reliefClassify 分类
     * @return 分类
     */
    @Override
    public List<LandClassifyVo> pageList(LandClassifyDto reliefClassify) {
        return baseMapper.pageList(reliefClassify);
    }

    /**
     * 查询分类列表
     *
     * @param
     * @return 分类集合
     */
    @Override
    public List<LandClassifyVo> findList() {
        return baseMapper.findList();
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param reliefClassify 分类列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelect> buildDeptTreeSelect(List<LandClassifyVo> reliefClassify) {
        List<LandClassifyVo> trees = buildDeptTree(reliefClassify);
        return trees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 构建前端所需要树结构
     *
     * @param reliefClassify 分类列表
     * @return 树结构列表
     */
    @Override
    public List<LandClassifyVo> buildDeptTree(List<LandClassifyVo> reliefClassify) {
        List<LandClassifyVo> returnList = new ArrayList<LandClassifyVo>();
        List<Long> tempList = new ArrayList<Long>();
        for (LandClassifyVo wiki : reliefClassify) {
            tempList.add(wiki.getId());
        }
        for (LandClassifyVo wiki : reliefClassify) {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(wiki.getParentId())) {
                recursionFn(reliefClassify, wiki);
                returnList.add(wiki);
            }
        }
        if (returnList.isEmpty()) {
            returnList = reliefClassify;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<LandClassifyVo> list, LandClassifyVo t) {
        // 得到子节点列表
        List<LandClassifyVo> childList = getChildList(list, t);
        t.setChildren(childList);
        for (LandClassifyVo tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<LandClassifyVo> getChildList(List<LandClassifyVo> list, LandClassifyVo t) {
        List<LandClassifyVo> tlist = new ArrayList<LandClassifyVo>();
        Iterator<LandClassifyVo> it = list.iterator();
        while (it.hasNext()) {
            LandClassifyVo n = (LandClassifyVo) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<LandClassifyVo> list, LandClassifyVo t) {
        return getChildList(list, t).size() > 0;
    }

    /**
     * 校验分类名称是否唯一
     *
     * @param reliefClassify 菜品分类
     * @return 结果
     */
    @Override
    public String checkNameUnique(LandClassify reliefClassify) {
        Long deptId = StringUtils.isNull(reliefClassify.getId()) ? -1L : reliefClassify.getId();
        LandClassifyVo info = reliefClassifyMapper.checkNameUnique(reliefClassify.getName(), reliefClassify.getParentId());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != deptId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 是否存在子节点
     *
     * @param id ID
     * @return 结果
     */
    @Override
    public boolean hasChildById(Long id) {
        int result = reliefClassifyMapper.hasChildById(id);
        return result > 0;
    }

    /**
     * 查询分类是否存在信息
     *
     * @param id ID
     * @return 结果 true 存在 false 不存在
     */
    @Override
    public boolean checkExistUser(Long id) {
        int result = reliefClassifyMapper.checkExistUser(id);
        return result > 0;
    }


    /**
     * 添加获取分类
     *
     * @param reliefClassify
     * @return 结果 true 存在 false 不存在
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean add(LandClassify reliefClassify) {
        return this.save(reliefClassify);
    }

    /**
     * 删除分类
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteById(Long id) {
        return this.removeById(id);
    }


    /**
     * 根据id和当前用户查询是否有权限修改
     *
     * @param id
     * @param userId 用户id
     * @return
     */
    @Override
    public boolean getExistence(Long id, Long userId) {
        return baseMapper.getExistence(id, userId);
    }
    /**
     * 以分类名为key返回id
     * @param
     * @return
     */
    @Override
    public Map<String, LandClassify> getLabelDicMap() {

        return this.list().stream().collect(Collectors.toMap(LandClassify::getName, (data) -> data, (k1, k2) -> k1));
    }

    /**
     * 以id为key返回分类名
     * @param
     * @return
     */
    @Override
    public Map<Long, LandClassify> getValueDicMap() {
        return this.list().stream().collect(Collectors.toMap(LandClassify::getId, (data) -> data, (k1, k2) -> k1));
    }
}
